package com.example.rabbitmqdemo.details;

import com.example.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 *
 * 主交换机无法路由就交给备用交换机
 *
 */
public class MySender2 {

    private static final String EXCHANGE_NAME = "my_exchange";
    private static final String ALTERNATE_EXCHANGE_NAME = "my_alternate_exchange";
    private static final String EXCHANGE_TYPE = "topic";
    private static final boolean EXCHANGE_DURABLE = true;
    private static final boolean EXCHANGE_AUTO_DELETE = false;
    private static final HashMap<String, Object> ARGUMENT = new HashMap<>();

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();

        ARGUMENT.put("alternate-exchange", ALTERNATE_EXCHANGE_NAME);

        channel.basicQos(32);
        channel.exchangeDeclare(ALTERNATE_EXCHANGE_NAME, "topic", EXCHANGE_DURABLE,
                EXCHANGE_AUTO_DELETE, null);
        channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, EXCHANGE_DURABLE,
                EXCHANGE_AUTO_DELETE, ARGUMENT);

        channel.basicPublish(EXCHANGE_NAME, "r1",
                new AMQP.BasicProperties.Builder()
                        .contentType("text/plain")
                        .deliveryMode(2)
                        .priority(2)
                        .expiration("10000")//设置单个消息过期时间
                        .userId("root")
                        .build(),
                "hello world".getBytes());

        channel.close();
        connection.close();
    }
}
